<?php
/**
 * @author tuadmin
 * @version 1.0.3
 */
class myXML extends SimpleXMLElement
{
	static private $optionMyXML_encoding = "UTF-8";
	static private $optionMyXML_version = "1.0";
	//static private $coding = "s";
	static private $optionsMyXML = array();
	public function __call($nombre,$arrays)
	{
		$padre = parent::addChild($nombre);
		foreach($arrays as $array)
		{
			foreach($array as $key => $value)
			{
				$padre->addChild($key, $value);
			}
		}
		return $padre;
	}
	public function __destruct()
	{
		self::$optionsMyXML = array();
	}
	public function addCDATA($cData)
	{
		$node = dom_import_simplexml($this);
		$no = $node->ownerDocument;
		$node->appendChild($no->createCDATASection($cData));
	}
	public function addChild($nombre , $value = null,$namespace = null)
	{
		if(is_array($value))
		{
			foreach($value as $val)
			{
				parent::addChild($nombre,$val,$namespace);
			}
		}
		else
		{
			return parent::addChild($nombre,$value,$namespace);
		}
	}
	public function setStyleSheet($tag,$attributes)
	{
		self::$optionsMyXML["stylesheet"][] = array(
		"tag" => $tag,
		"attributes" => $attributes
		);
	}
	public function setEncoding($string)
	{
		self::$optionMyXML_version = $string;
		return $this;
	}
	public function setVersion($string)
	{
		self::$optionMyXML_version = $string;
		return $this;
	}
	public function asXML($filename = null,$indentation = false,$separator = " ")
	{
		if($indentation == false && self::$optionMyXML_version =='1.0' && self::$optionMyXML_encoding == 'UTF-8')
		{return is_string($filename)?parent::asXML($filename):parent::asXML();}
		$dom = new DOMDocument('1.0');
		$dom->formatOutput = $indentation;
		$dom->preserveWhiteSpace = false;
		$dom->encoding = self::$optionMyXML_encoding;
		$dom->xmlVersion = self::$optionMyXML_version;
		if(isset(self::$optionsMyXML["stylesheet"]))
		{
			foreach(self::$optionsMyXML["stylesheet"] as $value)
			{
				$xslt = $dom->createProcessingInstruction($value['tag'], $value['attributes']);
				$dom->appendChild($xslt);
			}
		}
		$dom_sxe = $dom->importNode(dom_import_simplexml($this), true);
		$dom_sxe = $dom->appendChild($dom_sxe);

		if(is_string($filename))
		{
				if($separator != " ")
				{
					return file_put_contents($filename,$this->indentWithSeparator($dom->saveXML(),$separator));
				}
				return $dom->saveXML($filename);
		}
		else
		{
				if($separator != " ")
				{
					return $this->indentWithSeparator($dom->saveXML(),$separator);
				}
				return $dom->saveXML();
		}
	}
	/**
	 * convertir a html
	 * @param string $filename
	 * @param bool $indentation
	 * @param char $separator indent
	 * @return number|mixed|string
	 */
	public function asHTML($filename = null,$indentation = false,$separator = " ")
	{
		switch(true)
		{
			case $indentation == false:
				$doc = parent::asXML();
				if(substr($doc,37,2) == '?>'){
					$doc == substr($doc,39);
				}else{
					$doc == substr($doc,20);
				}
			break;
			case is_string($filename):
				return file_put_contents($filename,$doc);
			break;
			case $indentation == false:
				return $doc;
			break;
		}
		$dom = new DOMDocument('1.0');
		$dom->formatOutput = $indentation;
		$dom->preserveWhiteSpace = false;
		$dom->xmlVersion = 0;
		$dom_sxe = $dom->importNode(dom_import_simplexml($this), true);
		$dom_sxe = $dom->appendChild($dom_sxe);
		if(is_string($filename))
		{
				if($separator != " ")
				{
					return file_put_contents($filename,
					$this->indentWithSeparator(substr($dom->saveXML(),20),$separator)
											);
				}
				return substr($dom->saveXML($filename),20);
		}
		else
		{
				if($separator != " ")
				{
					return $this->indentWithSeparator(substr($dom->saveXML(),20),$separator);
				}
				return substr($dom->saveXML(),20);
		}
	}
	private function indentWithSeparator($xml,$separator)
	{
		return preg_replace("#\n( +)<#ise",'"\n".str_repeat("'.$separator.'",strlen("$1")/2)."<";',$xml);
	}
}